const Res = require("./baseRes");
const tools = require("./pub/tools");
let _data = {};

function createProxy( obj ){
    //设置代理
    return new Proxy( obj , {
        get( target, key, proxy ){
            const isProvise = key.indexOf("_")==0;
            if( target.hasOwnProperty(key) && !isProvise ) {
                return Reflect.get(target, key, proxy);
            }else if( target[key] && !isProvise ){
                return target[key].bind(target);
            }else if( target._data.hasOwnProperty(key) && !isProvise ){
                return Reflect.get(target._data, key, proxy);
            }

            return undefined;
        }
    })
}

class Manager{
    constructor(){
        this.__resNum = 0;
        this._data = _data;

        return createProxy( this );
    }

    say( str ){
        console.log( str || this._data );
    }

    //直接播放一个声音资源
    playVoice( resStr ){
        this.use( resStr )
        .then((res)=>{
            $T.playEffect( res );
        })
    }

    createPrefab( fabStr ){
        return cc.instantiate( this.g(fabStr) );
    }

    //获取某个或 多个资源
    use(){
        let args = Array.prototype.slice.call(arguments);
        return new Promise(( res , rej )=>{
            let keys , arr=[];
            for(let i in args){
                keys = tools.trimAll(args[i]) || "";
                let tarr = keys.split(",");
                for(let t in tarr){
                    arr.push(tarr[t])
                }
            }
            let loadnum = 0 , ret = {};

            const refn = ()=>{
                if( loadnum == arr.length ){
                    let re = ret
                    if( arr.length==1 ){
                        for(let k in ret){
                            re = ret[k];
                        }
                    }
                    res( re )
                }
            }

            for(let i in arr){
                let key = arr[i].split("#")[0];//去除备注
                if( this._data[key] ){
                    this._data[key].v.then(( d )=>{
                        loadnum++;
                        ret[key] = d;
                        refn();
                    });
                }else{
                    loadnum++;
                    ret[key] = null;
                    refn();
                }
            }
        });
    }
    //用户确定所用资源已经调入，则可直接返回
    g( keys ){
        keys = tools.trimAll(keys) || "";
        let arr = keys.split(",");
        let  ret = {} , re;

        for(let i in arr){
            let key = arr[i].split("#")[0];//去除备注
            if( this._data[key] ){
                ret[key] = this._data[key].res;
            }else{
                ret[key] = null;
            }
        }
        re = ret;
        if( arr.length==1 ){
            for(let k in ret){
                re = ret[k];
            }
        }
        return re;
    }

    //添加一个资源
    addResources( reses ){
        reses = reses || {};
        for(let key in reses){
            if( this._data[key] ) continue;
 
            let res = new Res(reses[key]);
            if(res){
                this._addResObject( key, res );
                if( reses[key].preload ) !!res.v; //预加载
            }
        }
    }

    _addResObject( key , res ){
        if( this._data[key] ) return;
        this._data[key] = res;
    }
}

module.exports = Manager; 